﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.Synchronization.Data;
using DevComponents.AdvTree;
using Microsoft.Synchronization.Data.SqlServer;
using System.Linq;
using DevComponents.DotNetBar.SuperGrid;
using SyncConfig.Dialog;
using AppCommon.Controls;
using AppCommon;
using SyncLib;
namespace SyncConfig.Controls
{
    public partial class TableOfScopeList : UserControlBase
    {
        
        public TableOfScopeList()
        {
            InitializeComponent();
        }
        public void DataBind()
        {
            try
            {
                SyncConfigDS.ScopeInfoExRow row = CurrentNode.Parent.Tag as SyncConfigDS.ScopeInfoExRow;
                DataView view = new DataView(FrmMain.MainForm.TreeManipulation.dsSyncConfig.TableOfScope);
                view.RowFilter = "ScopeExId='" + row.ScopeExId.ToString()+"'";
                superGridControl1.PrimaryGrid.DataSource = view;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        public override void Close()
        {
            superGridControl1.PrimaryGrid.DataSource = null;
            base.Close();
        }

        private void btnReloadFromScope_Click(object sender, EventArgs e)
        {
              SqlConnection conn = new SqlConnection(CommonVariable.SqlConnectionString);
              try
              {

                  conn.Open();

                  SyncConfigDS.ScopeInfoExRow row = CurrentNode.Parent.Tag as SyncConfigDS.ScopeInfoExRow;

                  DbSyncScopeDescription scopeDess = SqlSyncDescriptionBuilder.GetDescriptionForScope(row.ScopeName, "", row.Schema, conn);
                  SyncConfigDS.TableOfScopeDataTable tblData = FrmMain.MainForm.TreeManipulation.dsSyncConfig.TableOfScope;
                  int count = 0;
                  foreach (DbSyncTableDescription node in scopeDess.Tables)
                  {
                      if (ADOSyncConfig.ExistsTableInScopeEx(row.ScopeExId, node.UnquotedLocalName))
                          continue;

                      SyncConfigDS.TableOfScopeRow newRow = tblData.NewTableOfScopeRow();
                      DbSyncColumnDescription[] lstPry = node.PkColumns.ToArray<DbSyncColumnDescription>();
                      newRow.TableName = node.UnquotedLocalName;
                      newRow.ScopeExId = row.ScopeExId;
                      string temp = "";
                      if (lstPry.Length > 0)
                          foreach (DbSyncColumnDescription col in lstPry)
                          {
                              temp = temp + ";" + col.UnquotedName;
                          }
                      if (temp.Length > 0)
                          temp = temp.Substring(1);
                      newRow.TableId = Guid.NewGuid();
                      newRow.PrimaryKey = temp;
                      newRow.PreApplyCommand = "";
                      newRow.SolveConfigCommand = "";
                      newRow.SolveConfigType = "Auto";
                      newRow.AfterApplyCommand = "";
                      tblData.AddTableOfScopeRow(newRow);
                      count++;
                      FrmMain.MainForm.TreeManipulation.AdapterTableOfScope.Update(newRow);
                      CurrentNode.Nodes.Add(FrmMain.MainForm.TreeManipulation.CreateSingleTableOfScopeNode(null, newRow));
                      tblData.AcceptChanges();
                  }
                  MessageBox.Show("Đã cập nhật được " + count.ToString() + " bảng");
                  
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
              finally
              {
                  if (conn.State != ConnectionState.Closed)
                      conn.Close();
              }
        }

        private void superGridControl1_RowDoubleClick(object sender, DevComponents.DotNetBar.SuperGrid.GridRowDoubleClickEventArgs e)
        {
            btnDetail_Click(sender, e);
        }

        private void btnDetail_Click(object sender, EventArgs e)
        {
            if (superGridControl1.PrimaryGrid.ActiveRow != null)
            {

                SyncConfigDS.TableOfScopeRow row = superGridControl1.GetCurrentRow<SyncConfigDS.TableOfScopeRow>();

                Node[] existsNode = CurrentNode.Nodes.Find(row.TableName, false);
                if (existsNode.Length > 0)
                {

                    BaseDialog frm = new BaseDialog();
                    try
                    {
                        TableOfScopeInfo ctrl = new TableOfScopeInfo();
                        frm.Size = new Size(ctrl.Width + 20, ctrl.Height + 60);
                        ctrl.CurrentNode = existsNode[0];
                        ctrl.DataBind();
                        frm.panelEx1.Controls.Add(ctrl);
                        frm.ShowDialog();
                        ctrl.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {

                        frm.Dispose();
                    }
                }
            }
        }
    }
}
